﻿using BLL;
using DevExpress.XtraEditors;
using Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 纺织贸易管理系统;
using 纺织贸易管理系统.设置窗体;

namespace 后整理管理系统.报表窗体
{
    public partial class 检测报表 : Form
    {
        private SqlSugarClient _db = Connect.dbcontext;
        public 检测报表()
        {
            InitializeComponent();
            CreateGrid.Create (this.Name ,gridView1);
            gridControl1.ContextMenuStrip = contextMenuStrip1;
            uiDatePicker2.Value = DateTime.Now;
           uiDatePicker1.Value =uiDatePicker2.Value.AddDays(-QueryTime.间隔);
            Query();
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("新增检测单"))
            {
                新增窗体.检测单 form = new 新增窗体.检测单(FormUseful.新增,new TestData());
                form.ShowDialog();
                Query();
            }
        }
        private void Query()
        {
            var data = _db.Queryable<TestData,danjumingxitable>((x,y)=>x.DocNum==y.danhao).Where((x,y)=>y.ganghao.Contains(txtganghao.Text )&&x.CustomerName.Contains(txtkehu.Text )&&
            y.yanse .Contains(txtyanse.Text )&&y.pingming.Contains(txtpingming.Text) &&y.OrderNum.Contains(txtOrdernum.Text )&&
            x.DocumentDate>=uiDatePicker1.Value.Date &&x.DocumentDate<=uiDatePicker2.Value.Date.AddDays(1)&&x.DocNum.Contains("JC")&&x.ProductionOrderNo.Contains(txt流转卡.Text ))
                .Select((x,y)=>new {x,y.pingming ,y.yanse ,y.ganghao ,y.OrderNum ,y.chengpingmishu }).ToDataTable();
         
                gridControl1.DataSource = data;
           
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("修改检测单"))
            {
                var row=gridView1.GetFocusedDataRow () ;
                if (row != null)
                {
                    var doc = Connect.dbcontext.Queryable<TestData>().First(x => x.DocNum == row["x.TestData.DocNum"].ToString());
                    新增窗体.检测单 form = new 新增窗体.检测单(FormUseful.修改, doc);
                    form.ShowDialog();
                    Query();
                }
            }
        }

        private void txtkehu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) 
            {
                Query();
            }
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Query();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("删除检测单"))
            {
                if(ShowMessageBox.ShowAskBox("是否删除该单据？") == DialogResult.No)
                {
                    return;
                }
                var row = gridView1.GetFocusedDataRow() ;
                if (row != null)
                {
                    var docnum = row["x.TestData.DocNum"].ToString();
                    _db.Deleteable<TestData>().Where(x=>x.DocNum==docnum).ExecuteCommand();
                    _db.Deleteable<danjumingxitable>().Where(x => x.danhao == docnum).ExecuteCommand();
                    Query();
                }
            }
        }

        private void 配置列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fm= new 配置列(gridView1) { formname = this.Name, dt=gridControl1.DataSource as DataTable  };
            fm.ShowDialog();
        }

        private void 保存样式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateGrid.SaveGridview(this.Name ,gridView1);
        }
    }
}
